package com.magic.smartcity.model;

import android.app.Application;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.magic.smartcity.base.BaseApplication;
import com.magic.smartcity.beans.NewsCategoryBean;
import com.magic.smartcity.beans.PressBean;
import com.magic.smartcity.presenters.NewsPresenter;
import com.magic.smartcity.interfaces.INewsModel;
import com.magic.smartcity.model.network.NewsService;
import com.magic.smartcity.utils.HttpClient;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import dagger.Module;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import retrofit2.Retrofit;

/**
 * @description:
 * @create: Create by Magic on 2022/2/3.
 */


public class NewsModel implements INewsModel {
    private static final String TAG = NewsModel.class.getSimpleName();
    private Gson mGson = new Gson();

    @Inject
    Retrofit mRetrofit;

    @Inject
    NewsModel() {
    }

    @Override
    public void getNewsList(int type) {
        requstRetrofitNewsList(type);
    }

    @Override
    public void getTocationImages() {
        requstTocationImages();

    }

    @Override
    public void getNewsCategoryList() {
        requstNewsCategoryList();
    }

    /**
     * 请求新闻分类数据
     */
    private void requstNewsCategoryList() {
        NewsService newsService = mRetrofit.create(NewsService.class);
        newsService.getNewsCategoryListBean()
                .observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .doOnError(throwable -> {
                    throwable.printStackTrace();
                })
                .subscribe(newsCategoryBean -> {
                    EventBus.getDefault().post(
                            new NewsPresenter.NewsPresenterMessageEvent().setCategoryBeanList(newsCategoryBean.getData()));
                });

    }

    /**
     * 请求新闻轮播图数据
     */
    private void requstTocationImages() {
        NewsService newsService = mRetrofit.create(NewsService.class);
        newsService.getTocationImages()
                .observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .doOnError(throwable -> {
                    throwable.printStackTrace();
                })
                .subscribe(
                        tocationBean -> EventBus.getDefault().post(new NewsPresenter.NewsPresenterMessageEvent().setTocationList(tocationBean.getRows()))
                );
    }

    /**
     * 请求新闻列表数据
     */
    private void requstRetrofitNewsList(int type) {
        NewsService newsService = mRetrofit.create(NewsService.class);
        Observable<PressBean> call;
        if (type==-1){
            call = newsService.getNewsListBean("100", "1");//使用Rxjava搭配Retrofit
        }else {
            call = newsService.getNewsListBean("100", "1",type+"");//使用Rxjava搭配Retrofit
            Log.d(TAG,"请求的新闻类型:::"+type);
        }

        call.observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .doOnError(throwable -> {
                    throwable.printStackTrace();
                })
                .subscribe(
                        pressBean ->
                                EventBus.getDefault().post(new NewsPresenter.NewsPresenterMessageEvent().setPressList(pressBean.getRows()))

                );


    }

}
